package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.Entity.Dish;
import com.sky.Entity.Flavor;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishQueryPageDTO;
import com.sky.exception.HasSetMealException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.service.DishService;
import com.sky.vo.DishVo;
import com.sky.vo.PageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private FlavorMapper flavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    @Transactional
    public void insert(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.DISABLE);
        Integer result=dishMapper.insert(dish);
        List<Flavor> flavors = dishDTO.getFlavors();
        if (flavors != null&&flavors.size()>0) {
            for (Flavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            flavorMapper.insert(flavors);
        }
    }

    @Override
    public PageVo<Dish> queryPage(DishQueryPageDTO dishQueryPageDTO) {
        PageHelper.startPage(dishQueryPageDTO.getPage(), dishQueryPageDTO.getPageSize());
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishQueryPageDTO,dish);
        Page<Dish> list=dishMapper.queryPage(dish);

        PageVo<Dish> result=new PageVo<>();
        result.setRecords(list.getResult());
        result.setTotal(list.getTotal());

        return result;
    }

    @Override
    public void delete(List<Long> ids) {
        int count=setmealDishMapper.queryCount(ids);
        System.out.println("count="+count);
        if(count>0){
            throw new HasSetMealException("有关联的套餐");
        }
        dishMapper.delete(ids);
        flavorMapper.delete(ids);
    }

    @Override
    public DishVo queryById(Long id) {
        Dish dish=dishMapper.queryById(id);
        List<Flavor> list=flavorMapper.queryByDishId(id);
        DishVo dishVo=new DishVo();
        BeanUtils.copyProperties(dish,dishVo);
        dishVo.setFlavors(list);
        return dishVo;
    }

    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        List<Flavor> flavors = dishDTO.getFlavors();
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        if (flavors != null&&flavors.size()>0) {
            //删除菜品下的口味
            flavorMapper.deleteByDishId(dish.getId());

            for (Flavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }

            //插入口味数据
            flavorMapper.insert(flavors);
        }
    }

    @Override
    public List<Dish> getDishsById(Long categoryId) {
        return dishMapper.queryByCateGoryId(categoryId);
    }

    @Override
    public List<DishVo> queryDishByCategoryId(Long categoryId) {
        List<Dish> dishes = dishMapper.queryByCateGoryId(categoryId);
        if (dishes == null) {
            return null;
        }
        List<DishVo> list=new ArrayList<>();
        for (Dish dish : dishes) {
            List<Flavor> flavors = flavorMapper.queryByDishId(dish.getId());
            DishVo dishVo=new DishVo();
            BeanUtils.copyProperties(dish,dishVo);
            dishVo.setFlavors(flavors);
            list.add(dishVo);
        }
        return list;
    }
}









































































